<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\home\controller;

class CourseController extends BaseController
{

	/**
	*课程-查看详情页面
	*
	**/
	public function details()
	{
		$course_id = $this->request->param('course_id', '');
        
		if(empty($course_id)){
	       return $this->error('参数有误，非法请求');
		}

		//开始查找课程
		$courseModel = new \app\home\model\CourseModel();
    	$coulabelModel = new \app\home\model\CourseLabelModel();
        $couageModel = new \app\home\model\CourseAgeModel();
        $cousubjectModel = new \app\home\model\CourseSubjectModel();
        $keyModel = new \app\home\model\CourseKeyModel();
        $valueModel = new \app\home\model\CourseValueModel();
        $ckModel = new \app\home\model\CourseSkuModel();

        //$str = 'id, title, long_title, inventory_number, sell_money, original_money, basic_number, sales_number, zt_logo, js_logo, buy_notice, start_time, end_time, explain';
        $str = 'id, title, basic_number, sales_number, zt_logo, js_logo, buy_notice, start_time, end_time, explain';
        $info = $courseModel->getCourseInfoDetails($course_id, $str);
        if(empty($info))
        {
        	 return $this->error('该课程已下架');
        }

        //第三版，规格下单 - 最低价规格的价格
        $sku_info = $ckModel->getCourseSkuLowestPriceInfo($info['id']);
        $info['sell_money'] = $sku_info['sell_money'];
        $info['original_money'] = $sku_info['original_money'];
        $info['inventory_number'] = $sku_info['inventory_number'];

        //1.获得课程的属性，属性值，SKU
        $key_list = $keyModel->getCourseKeyList($info['id']);
        foreach ($key_list as $key => $value) {
            //再找出VALUE属性值
            $value_list = $valueModel->getCourseValueListColumn($value['id']);
            $key_list[$key]['value_name'] = $value_list;
            
        }

        $memModel = new \app\home\model\MemberModel();
        $accessModel = new \app\home\model\MemberAccessModel();
        //先判断自己是否有上级
        $member_id = $this->member_id;
        $invite_code = $this->request->param('invite_code', '');//海报自带的邀请码
        $member_info = $memModel->getMemberIdWhereMemberId($member_id, 'member_id, nickname, parent, grade');

        //存在邀请码+未绑定上级，进行判断处理
        if(!empty($invite_code) && empty($member_info['parent']))
        {
            //查找邀请人信息
            $where = [
                ['invite', '=', $invite_code]
            ];
            $pinfo = $memModel->geetMemberWhereReturnInfo($where, 'member_id, open_id, nickname, logo, grade');
            //存在邀请人，并且为星级顾问
            if(!empty($pinfo) && ($pinfo['grade'] >= 2) &&($member_id != $pinfo['member_id']))
            {
                //判断邀请人是否为该用户的下级
                //$res = $this->getMemberIfInviteTop($pinfo['member_id']);
                //if(!$res)
                //{
                    //邀请人不是B的下级，增加一条访客信息
                    $add = [
                        'member_id' => $pinfo['member_id'],
                        'access_id' => $member_id,
                    ];
                    $accessModel->addAccessRes($add);

                    //绑定访客，通知消息
                    $notice_contro = new NoticeController();
                    $send_data = [
                        'first' => ['value'=>'有新访客访问了您的分享', 'color'=>"#173177"],
                        'keyword1' => ['value'=> $member_info['nickname'], 'color'=>'#173177'],
                        'keyword2' => ['value'=> $info['title'], 'color'=>'#173177'],
                        'keyword3' => ['value'=> date('Y-m-d H:i:s'), 'color'=>'#173177'],
                        'remark' => ['value'=> '好友购课后您可以获得收益', 'color'=>'#173177'],
                    ];
                    $return_url = WEB_URL."/my_team?type=3";
                    $notice_contro->sendNoticeMessage('fangke', $send_data, $return_url, '', $pinfo['member_id']);

                    $this->assign('invite', $invite_code);
                //}
            }else{
                $this->assign('invite', 0);
            }
        }else{
            $this->assign('invite', 0);
        }

    	$info['label'] = $coulabelModel->getCourseLabelList($info['id'], 'id,label');

    	$info['zt_logo'] = json_decode($info['zt_logo']);//'/upload/'.$info['zt_logo']
    	foreach ($info['zt_logo'] as $key => $value) {
    		$zj_logo[] = json_img_add_url('/upload/'.$value);
    	}
    	$info['zt_logo'] = $zj_logo;


    	$info['js_logo'] = json_decode($info['js_logo']);
    	foreach ($info['js_logo'] as $key => $value) {
    		$js_logo[] = json_img_add_url('/upload/'.$value);
    	}
    	$info['js_logo'] = $js_logo;

    	$info['buy_notice'] = json_img_add_url(htmlspecialchars_decode($info['buy_notice']));

        $info['grade'] = $member_info['grade'];

        $this->assign('info', $info);
        $this->assign('key_list', $key_list);//规格列表

        $info['explain'] = explode(PHP_EOL, $info['explain']);
        return $this->fetch('course/index');
	}




	/**
	*购买课程页面
	*
	**/
	public function addOrders()
	{
		$course_id = $this->request->param('course_id', '');
		if(empty($course_id)){
	       return $this->error('参数有误，非法请求');
		}


        $sku_id = $this->request->param('sku_id', '');
        if(empty($sku_id)){
           return $this->error('请选择需要的课程规格');
        }

		//开始查找课程
		$courseModel = new \app\home\model\CourseModel();
    	$coulabelModel = new \app\home\model\CourseLabelModel();
        $couageModel = new \app\home\model\CourseAgeModel();
        $cousubjectModel = new \app\home\model\CourseSubjectModel();
        $accessModel = new \app\home\model\MemberAccessModel();
        $ckModel = new \app\home\model\CourseSkuModel();

        //$str = 'id, title, long_title, inventory_number, sell_money, original_money, basic_number, sales_number, xt_logo, start_time, end_time, explain,status, attend_type, activate_method, activate_time,refund, if_collection';
        $info = $courseModel->getCourseInfoDetails($course_id);
        if(empty($info)||($info['status'] != 1))
        {
        	return $this->error('该课程已下架');
        }

        //课程结束时间，对比当前时间
        $time = time();
        $start_time = strtotime($info['start_time']);
        $end_time = strtotime($info['end_time']);

        //判断课程是否开始售卖时间
        if(($time > $end_time) || ($time < $start_time))
        {
 			return $this->error('该课程不在售卖期');
        }

        $info['xt_logo'] = json_img_add_url('/upload/'.$info['xt_logo']);


        //找出规格的信息
        $sku_info = $ckModel->getCourseSkuIdInfo($course_id, $sku_id);
        if(empty($sku_info))
        {
            return $this->error('找不到对应的规格信息,请刷新页面再试');
        }

        //对应规格，对应的价格
        $info['sku_id'] = $sku_id;
        $info['specs'] = $sku_info['specs'];
        $info['sell_money'] = $sku_info['sell_money'];


        //查找用户最近的一次订单收货信息
        $member_id = $this->member_id;
        $orderModel = new \app\home\model\OrdersModel();
        $order_info = $orderModel->getMemberOrderAddtimeAsc($member_id, 'recipients, mobile, province, city, district, site');
        if(!empty($order_info))
        {
            $info['recipients'] = $order_info['recipients'];
            $info['mobile'] = $order_info['mobile'];
            $info['province'] = $order_info['province'];
            $info['city'] = $order_info['city'];
            $info['district'] = $order_info['district'];
            $info['site'] = $order_info['site'];
        }

        //是否存在邀请码
        $memModel = new \app\home\model\MemberModel();
        $member_id = $this->member_id;
        $invite_code = $this->request->param('invite_code', '');//海报自带的邀请码
        $member_info = $memModel->getMemberIdWhereMemberId($member_id, 'member_id, parent, grade');

        //存在邀请码+未绑定上级，进行判断处理
        if(!empty($invite_code) && ($invite_code != 0) && empty($member_info['parent']))
        {
            //查找邀请人信息
            $where = [
                ['invite', '=', $invite_code]
            ];
            $pinfo = $memModel->geetMemberWhereReturnInfo($where, 'member_id, nickname, grade, logo');
            //存在邀请人，并且为星级顾问
            if(!empty($pinfo) && ($pinfo['grade'] >= 2) &&($member_id != $pinfo['member_id']))
            {
                //展示邀请人信息
               $this->assign('pinfo', $pinfo);
            }
        }else{
            //如果没有邀请码，那就找最近访问的人
            $assinfo = $accessModel->getMemberAccessOthers($member_id);
            if(!empty($assinfo) && empty($member_info['parent']))
            {
                $where = [
                    ['member_id', '=', $assinfo['member_id']]
                ];
                $pinfo = $memModel->geetMemberWhereReturnInfo($where, 'member_id, nickname, grade, logo');
                //展示邀请人信息
               $this->assign('pinfo', $pinfo);
            }
        }

        
		$this->assign('info', $info);
        return $this->fetch('course/add_order');
	}



    /**
    *
    *查看上课方式
    **/
    public function lookCourseWay()
    {
        $member_id = $this->member_id;
        $course_id = $this->request->param('course_id', '');
        $order_id = $this->request->param('order_id', '');
        if(empty($course_id)){
           return $this->error('参数有误，非法请求');
        }


        //开始查找课程
        $courseModel = new \app\home\model\CourseModel();
        $orderModel = new \app\home\model\OrdersModel();
        $coursecodeModel = new \app\home\model\CourseCodeModel();


        $str = 'id, title, attend_type, buy_notice, status,activate_method,activate_time';
        $info = $courseModel->getCourseInfoDetails($course_id, $str);
        if(empty($info))
        {
             return $this->error('该课程已下架');
        }
        $info['buy_notice'] = json_img_add_url(htmlspecialchars_decode($info['buy_notice']));

        //如果有订单ID
        if(!empty($order_id))
        {
            $where = [
                ['member_id', '=', $member_id],
                ['order_id', '=', $order_id],
                ['status', 'in', [1,2,3,4,5]],
            ];

            $oinfo = $orderModel->memberOrderInfo($where);
            
            if(empty($oinfo))
            {
                $this->error('不存在的订单信息，请确认后操作');
            }

            //查找兑换码
            if(!empty($oinfo['course_code_id']))
            {
                $code_info = $coursecodeModel->getOrderCodeInfo($oinfo['course_code_id'], 'id, code_name');
                $oinfo['code_name'] = $code_info['code_name'];
            }

            $this->assign('oinfo', $oinfo);
        }

        $this->assign('info', $info);
        return $this->fetch('course/note_class');
    }



	/**
	*
	*课程 - 生成订单,拉起支付
	**/
	public function payCourse() 
	{
		//接收数据，检测数据
        $member_id = $this->member_id;
		$data = $this->request->param();
        
        //检测参数
        $validate = new \app\home\validate\CourseValidate();
        if(!$validate->scene('pay')->check($data))
        {
            return return_msg(2001, $validate->getError());
        }

        //判断手机号
        if(!cmf_check_mobile($data['mobile']))
        {
            return return_msg(2002, '请输入正确的手机号码');
        }


        //查找课程价格
        $courseModel = new \app\home\model\CourseModel();
        $ckModel = new \app\home\model\CourseSkuModel();
        //$str = 'id, title, sell_money, postage, commission, inventory_number, start_time, end_time, status, bonus_rebate, if_collection,if_birthday,if_nickname,if_site';

        $str = 'id, title, postage, start_time, end_time, status, bonus_rebate, if_collection,if_birthday,if_nickname,if_site';
        $cinfo = $courseModel->getCourseInfoDetails($data['course_id'], $str);
        if(empty($cinfo)||($cinfo['status'] != 1))
        {
             return return_msg(2003, '该课程已下架');
        }


        //找出对应规格的信息
        $sku_info = $ckModel->getCourseSkuIdInfo($data['course_id'], $data['sku_id']);
        if(empty($sku_info))
        {
            return $this->error('找不到对应的规格信息,请刷新页面再试');
        }

        //对应规格，对应的价格
        $cinfo['sku_id'] = $data['sku_id'];
        $cinfo['order_sku'] = $sku_info['specs'];
        $cinfo['sell_money'] = $sku_info['sell_money'];
        $cinfo['commission'] = $sku_info['commission'];
        $cinfo['inventory_number'] = $sku_info['inventory_number'];

        //课程结束时间，对比当前时间
        $time = time();
        $start_time = strtotime($cinfo['start_time']);
        $end_time = strtotime($cinfo['end_time']);

        //判断课程是否开始售卖时间
        if(($time > $end_time) || ($time < $start_time))
        {
            return return_msg(2004, '该课程不在售卖期');

        }

        //课程库存
        if($cinfo['inventory_number'] <= 0)
        {
            return return_msg(2005, '该课程已售罄');
        }


        //开始生成订单，并拉起微信支付
        $orderModel = new \app\home\model\OrdersModel();

        //生成订单编号
        $order_number = date('YmdHis');
        $order_number .= $this->random_string(6);

        //省，市，区
        $china = explode(',', $data['province']);
        
        $add_data = [
            'member_id' => $member_id,
            'order_number' => $order_number,
            'course_id' => $data['course_id'],
            'recipients' => $data['recipients'],
            'mobile' => $data['mobile'],
            'province' => $china[0],
            'city' => $china[1],
            'district' => !empty($china[2]) ? $china[2] : '',
            'site' => $data['site'],
            'pay_money' => $cinfo['sell_money'],//售价
            'commission' => $cinfo['commission'],//佣金
            'bonus_rebate' => $cinfo['bonus_rebate'],//分红比例
            'pay_number' => 1,

            'order_sku' => $cinfo['order_sku'],//所选规格
        ];

        //需要收集的话，就是必填的
        if(($cinfo['if_collection'] == 1) && empty($data['baby_age']))
        {
            return return_msg(200501, '请填写宝宝的年龄');
        }

        if(($cinfo['if_birthday'] == 1) && empty($data['baby_birthday']))
        {
            return return_msg(200502, '请填写宝宝的生日');
        }

        if(($cinfo['if_nickname'] == 1) && empty($data['baby_name']))
        {
            return return_msg(200503, '请填写宝宝的昵称');
        }

        if(($cinfo['if_site'] == 1) && (empty($data['province']) || empty($data['site'])))
        {
            return return_msg(200504, '请填写收货的地址');
        }


        $add_data['baby_age'] = $data['baby_age'];
        $add_data['baby_birthday'] = $data['baby_birthday'];
        $add_data['baby_name'] = $data['baby_name'];

        $res = $orderModel->addMemberOrders($add_data);
        if(empty($res))
        {
            return return_msg(2006, '生成课程订单失败，系统错误');
        }

        $memModel = new \app\home\model\MemberModel();
        $mem_info = $memModel->getMemberIdWhereMemberId($member_id, 'open_id');

        //统一下单
        $body = '购买课程';
        $out_trade_no = $add_data['order_number'];

        //价格+运费
        $total_fee = ($cinfo['sell_money'] + $cinfo['postage']) * 100;


        //$total_fee = 1;
        
        $notify_url = WEB_URL.'/pay_back';

        $wx = new WxController();
        $order = $wx->getPrePayOrder($body, $out_trade_no, $total_fee, $notify_url, $mem_info['open_id']);
        if ($order['return_code'] == 'SUCCESS'){
            if(empty($order['prepay_id']))
            {
                WL([ 'msg' => 'test', 'order' => $order],'wx_h5_pay');
                return return_msg(2009, $order['err_code_des']);
            }

            $order1 = $wx->getOrder($order['prepay_id']);//执行二次签名返回参数

            $order1['order_id'] = $res;
            WL([ 'msg' => '生成预付款订单成功'],'wx_h5_pay');
            return return_msg(200, '生成预付款订单成功', $order1);
        } else {
            return return_msg(2008, $order['return_msg']);
        }
	}



    /**
    *
    *订单成功页面
    **/
    public function orderSuccess()
    {
        $member_id = $this->member_id;

        $orderModel = new \app\home\model\OrdersModel();
        $order_id = $this->request->param('order_id', '');
        if(empty($order_id))
        {
            $this->error('订单ID错误，参数非法');
        }

        //开始查找
        $where = [
            ['member_id', '=', $member_id],
            ['order_id', '=', $order_id],
        ];

        $info = $orderModel->memberOrderInfo($where, 'order_id, member_id');
        if(empty($info))
        {
            $this->error('不存在的订单信息，请确认后操作');
        }
        $this->assign('info', $info);
        return $this->fetch('course/pay');
    }


    /**
    *
    *课程-奖励介绍页面
    *
    **/
    public function courseAwardIntroduce()
    {   
        $course_id = $this->request->param('course_id', '', 'intval');//课程ID
        if(empty($course_id))
        {
            return return_msg(2001, '参数有误，非法请求');
        }


        $cModel = new \app\home\model\CourseModel();
        $conversModel = new \app\home\model\CourseConversionModel();
        $keyModel = new \app\home\model\CourseKeyModel();
        $valueModel = new \app\home\model\CourseValueModel();
        $ckModel = new \app\home\model\CourseSkuModel();

        //查找课程
        //$course_info = $cModel->getCourseInfoDetails($course_id, 'id, title, sell_money, commission, one_rebate, two_rebate');
        $course_info = $cModel->getCourseInfoDetails($course_id, 'id, title, one_rebate, two_rebate');
        if(empty($course_info))
        {
            return return_msg(2002, '课程已下架或未上架');
        }

        //按照规格展示对应的数据
        $arr = [];//总数据
        $keys_arr = [];
        $keys_list = $keyModel->getCourseKeyList($course_id);
        foreach ($keys_list as $key => $value) {
            //先组织属性
            $keys_arr[] = $value['name'];
        }
        $arr['key'] = $keys_arr;

        //再直接找SKU表
        $sku_list = $ckModel->getCourseAllList($course_id);
        foreach ($sku_list as $key => $value) {
                $sku_list[$key]['values'] = json_decode($value['specs'], true);
                unset($sku_list[$key]['specs']);

                //佣金计算
                $sku_list[$key]['one'] = editNumber($value['commission'] * $course_info['one_rebate']);
                $sku_list[$key]['two'] = editNumber($value['commission'] * $course_info['two_rebate']);
        }  

        $arr['values'] = $sku_list;

        //小课转大课
        $list = $conversModel->getCourseInfo($course_id);

        $this->assign('course_info', $course_info);
        $this->assign('sku_arr', $arr);
        $this->assign('list', $list);
        return $this->fetch('course/promotion_reward');
    }



    /**
    *
    *课程分享-短链接生成
    *
    **/
    public function courseCreateShortLink()
    {
        $course_id = $this->request->param('course_id', '', 'intval');//课程ID
        if(empty($course_id))
        {
            return return_msg(2001, 'id有误，非法请求');
        }
        
        $cModel = new \app\home\model\CourseModel();
        $Model = new \app\home\model\CourseSlinkModel();
        $memModel = new \app\home\model\MemberModel();
       

        //查找课程
        $course_info = $cModel->getCourseInfoDetails($course_id, 'id, title');
        if(empty($course_info))
        {
            return return_msg(2002, '参数有误，非法请求');
        }

        //第三版，规格下单 - 最低价规格的价格
        $ckModel = new \app\home\model\CourseSkuModel();
        $sku_info = $ckModel->getCourseSkuLowestPriceInfo($course_id);
        $course_info['sell_money'] = $sku_info['sell_money'];

        //查找用户信息
        $member_id = $this->member_id;
        $info = $memModel->getMemberIdWhereMemberId($member_id, 'member_id, invite');
 

        //短链默认url
        $short_url = 'http://d.fenchuang100.com/a/';


        //用户ID，去数据库查找
        $where = [
            ['course_id', '=', $course_id],
            ['member_id', '=', $this->member_id],
        ];
        $con_info = $Model->getMemberShortLink($where);
        if(empty($con_info))
        {
             //没有，生成短链
            $vcode = $this->addShortLink();
            $url = WEB_URL.'/course_details?course_id='.$course_id.'&invite_code='.$info['invite'];

            //添加
            $add_data = [
                'member_id' => $member_id,
                'course_id' => $course_id,
                'vcode' => $vcode,
                'url' => $url,
            ];

            $res = $Model->addShortLink($add_data);
            if(empty($res))
            {
                return return_msg(2003, '生成短链失败，系统错误');
            }

            //返回短链
            $short_url .= $add_data['vcode'];
            return return_msg(200, 'ok', ['short_url' => $short_url, 'course_info' => $course_info]);


        }else{
            //已存在，直接返回
            $short_url .= $con_info['vcode'];
            return return_msg(200, 'ok', ['short_url' => $short_url, 'course_info' => $course_info]);
        }   

    }




    /**
    *
    *生成-短链随机数
    *
    **/
    protected function addShortLink()
    {
        $vcode = cmf_random_string();

        $Model = new \app\home\model\CourseSlinkModel();
        $where = [
            ['vcode', '=', $vcode],
        ];
        $info = $Model->getMemberShortLink($where);
        if(!empty($info))
        {
            //回调本函数
            $vcode = $this->addShortLink();
            return $vcode;
        }else{
            return $vcode;
        }
    }




    /**
    *
    *分享-课程资料
    *
    **/
    public function shareCourse()
    {
        $course_id = $this->request->param('course_id', '', 'intval');//课程ID
        if(empty($course_id))
        {
            return return_msg(2001, '参数有误，非法请求');
        }

        $memModel = new \app\home\model\MemberModel();
        $cModel = new \app\home\model\CourseModel();
        $ckModel = new \app\home\model\CourseSkuModel();
        //查找课程
        //$course_info = $cModel->getCourseInfoDetails($course_id, 'id, title, sell_money, zt_logo, qm_zt_logo');
        $course_info = $cModel->getCourseInfoDetails($course_id, 'id, title, sell_money, zt_logo, qm_zt_logo');
        if(empty($course_info))
        {
            return return_msg(2002, '课程已下架或未上架');
        }

        //第三版，规格下单 - 最低价规格的价格
        $sku_info = $ckModel->getCourseSkuLowestPriceInfo($course_id);
        $course_info['sell_money'] = $sku_info['sell_money'];


        $zt_logo = json_decode($course_info['zt_logo']);
        $course_info['tj_logo'] = json_img_add_url('/upload/'.$zt_logo[0]);

        $content = file_get_contents($course_info['tj_logo']);
        $course_info['qm_zt_logo'] = base64_encode($content);
        


        //查找用户信息
        $member_id = $this->member_id;
        $info = $memModel->getMemberIdWhereMemberId($member_id, 'member_id, logo, nickname, grade, invite');

        //海报链接
        $info['beijing'] = WEB_URL.'/upload/default/20200717/0717yqhy.png?v='.time();

        return return_msg(200, 'ok', ['course_info' => $course_info, 'info' => $info]);
    }



    /**
    *
    *分享-课程二维码
    *
    **/
    public function courseQrcode()
    {
        //分享链接，编程二维码
        include_once CMF_ROOT.'vendor/phpqrcode/phpqrcode.php';
        
        $course_id = $this->request->param('course_id', '', 'intval');//课程ID
        if(empty($course_id))
        {
             return return_msg(2001, '参数有误，非法请求');
        }
        //查找课程
        $cModel = new \app\home\model\CourseModel();
        $course_info = $cModel->getCourseInfoDetails($course_id, 'id, title');
        if(empty($course_info))
        {
            return return_msg(2002, '参数有误，非法请求');
        }

        //查找用户信息
        $member_id = $this->member_id;
        $memModel = new \app\home\model\MemberModel();
        $info = $memModel->getMemberIdWhereMemberId($member_id, 'member_id, invite');
        $url = WEB_URL.'/course_details?course_id='.$course_id.'&invite_code='.$info['invite'];

        //图片链接，一起返回
        $name = date('Ymd').cmf_random_string_two(12).'.png';
        $outfile = CMF_ROOT.'public'.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.date('Ymd').'course'.DIRECTORY_SEPARATOR.$name;
        $outfile = false;
        $level = 'H';
        $size =4;

        /*if (!file_exists(dirname($outfile))){
            mkdir(dirname($outfile), 0777, true);
        }

        if(file_exists($outfile)){
            unlink($outfile);
        }*/

        $qrcode = new \QRcode();
        ob_start();
        $qrcode::png($url, $outfile, $level, $size);  
        //$path = substr($outfile, 1);

        //这里就是把生成的图片流从缓冲区保存到内存对象上，使用base64_encode变成编码字符串，通过json返回给页面。
        $imageString = base64_encode(ob_get_contents());

        //关闭缓冲区
        ob_end_clean();
        //把生成的base64字符串返回给前端
        return return_msg(200, 'ok', $imageString);
    }




    protected function random_string($len = 6)
    {
        $chars    = [
            "1","2","3","4","5","6","7","8","9","0"
        ];
        $charsLen = count($chars) - 1;
        shuffle($chars);    // 将数组打乱
        $output = "";
        for ($i = 0; $i < $len; $i++) {
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }


    /**
    *用户是否为邀请人的上级
    *
    **/
    protected function getMemberIfInviteTop($parent_member_id)
    {
        $member_id = $this->member_id;
        $memModel = new \app\home\model\MemberModel();
        $pinfo = $memModel->getMemberIdWhereMemberId($parent_member_id, 'member_id, parent');
        //存在上级
        if(!empty($pinfo['parent']))
        {
            //邀请人的上N级=当前用户
            if($pinfo['member_id'] == $member_id)
            {
                return true;
            }else{
                $res = $this->getMemberIfInviteTop($pinfo['member_id']);
                return $res;
            }
            
        }else{
            return false;
        }
    }
}







